package cn.elwy.rcp.kit.handlers;

import org.eclipse.e4.core.di.annotations.CanExecute;
import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.core.di.annotations.Optional;
import org.eclipse.e4.core.services.events.IEventBroker;
import org.eclipse.e4.ui.workbench.modeling.EPartService;

import cn.elwy.rcp.kit.event.MyEventConstants;

public class SaveHandler {

  @Execute
  public void execute(EPartService partService, IEventBroker broker) {
    if (partService != null) {
      partService.saveAll(false);
      broker.post(MyEventConstants.TOPIC_TODO_UPDATE, "saved");
    }
  }

  @CanExecute
  boolean canExecute(@Optional EPartService partService) {
    if (partService != null) {
      return !partService.getDirtyParts().isEmpty();
    }
    return false;
  }

}